Python C API return more than one value / object without building a tuple [migrated]

Posted by Grisu on Programmers See other posts from Programmers or by Grisu
Published on 2013-11-05T07:55:57Z Indexed on 2013/11/05 16:11 UTC
Read the original article Hit count: 386

Filed under:
|

I got the following problem. I have written a C-Extension to Python(2.7 / 3.2) to interface a self written software library. Unfortunately I need to return two values from the function where the last one is optional. In Python I tried

def func(x,y): 
    return x+y, x-y

test = func(13,4)    

but test is a tuple. If I write

test1,test2 = func(13,4)    

I got both values separated. Is there a possibility to return only one value without unpacking the tuple, i.e. the second(,.. third, ..fourth) value gets neglected?

And if such a solution existst, how does it look for the C-API? Because

return Py_BuildValue("ii",x+y,x-y); 

results in a tuple as well.

© Programmers or respective owner

Related posts about python

Related posts about c